<template>
  <view>
    <my-search bgcolor="#cd78c4"></my-search>
    <view class="scroll-box">
      <!-- 左侧大盒子 -->
      <scroll-view
        class="left-box"
        scroll-y="true"
        :style="{ height: wh + 'px' }"
      >
        <!-- 列表渲染左侧的每个item项 -->
        <view
          :class="['left-item', index === activeIndex ? 'active' : '']"
          v-for="(item, index) in cateList"
          :key="index"
          @click="changeActive(index)"
        >
          {{ item.cat_name }}
        </view>
      </scroll-view>
      <!-- 右侧大盒子 -->
      <scroll-view
        class="right-box"
        scroll-y="true"
        :scroll-top="scrollTop"
        :style="{ height: wh + 'px' }"
      >
        <!-- 二级分类 -->
        <view
          class="cate-lv2"
          v-for="(item2, index2) in cateList2"
          :key="index2"
        >
          <!-- 标题 -->
          <view class="cate-lv2-title">/ {{ item2.cat_name }} /</view>
          <!-- 三级分类 -->
          <view class="cate-lv3-list">
            <view
              class="cate-lv3-item"
              v-for="(item3, index3) in item2.children"
              :key="index3"
              @click="goGoodsList(item3.cat_id)"
            >
              <image :src="item3.cat_icon" mode=""></image>
              <text>{{ item3.cat_name }}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>
<script>
import badgeMix from '@/mixins/tabbar-badge.js';
export default {
  mixins: [badgeMix],
  data() {
    return {
      wh: 0,
      // 分类列表，一级分类
      cateList: [],
      // 二级分类
      cateList2: [],
      //当前选中的这一项
      activeIndex: 0,
      // 设置竖向滚动条位置
      scrollTop: 0
    };
  },
  onLoad() {
    // 50:自定义的搜索组件的高度
    this.wh = uni.getSystemInfoSync().windowHeight - 50;
    // console.log(uni.getSystemInfoSync().windowHeight);
    this.getCateList();
  },
  methods: {
    // 点击三级分类 跳转到商品列表页
    goGoodsList(id) {
      uni.navigateTo({
        url: '/subpkg/goods_list/goods_list?cid=' + id
      });
    },
    // 切换一级分类的tab
    changeActive(i) {
      this.activeIndex = i;
      this.cateList2 = this.cateList[i].children;
      this.scrollTop = this.scrollTop === 0 ? 1 : 0;
    },
    // 请求分类列表的函数
    async getCateList() {
      const { data } = await uni.$http.get('/api/public/v1/categories');
      console.log(data);
      if (data.meta.status !== 200) {
        return uni.$showMsg();
      }
      this.cateList = data.message;
      this.cateList2 = data.message[0].children;
    }
  }
};
</script>

<style lang="scss">
.scroll-box {
  display: flex;
  .left-box {
    width: 120px;
    .left-item {
      line-height: 60px;
      background-color: #efefef;
      text-align: center;
      font-size: 12px;
      &.active {
        background-color: #fff;
        position: relative;
        &::before {
          display: block;
          content: '';
          width: 2px;
          height: 30px;
          background-color: #c00000;
          position: absolute;
          // top: 50%;
          // left: 0;
          // transform: translateY(-50%);
          top: 0;
          bottom: 0;
          margin: auto;
        }
      }
    }
  }
  .right-box {
    .cate-lv2-title {
      font-weight: bold;
      text-align: center;
      font-size: 12px;
      padding: 15px 0;
    }
    .cate-lv3-list {
      display: flex;
      flex-wrap: wrap;
      .cate-lv3-item {
        width: 33.3%;
        display: flex;
        flex-direction: column;
        align-items: center;
        image {
          width: 50px;
          height: 50px;
        }
        text {
          font-size: 12px;
        }
      }
    }
  }
}
</style>
